/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package InterfaceGraphique.commonPannel;

import InterfaceGraphique.FenJeuEntrer;
import cartes.Carte;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author arthur.hoareau
 */
public class PanListeCarte extends JPanel {
        
    protected HashMap<PanCarte, Carte> cartes;
    protected JPanel pText;
    protected JPanel pCartes;
    protected JLabel desc;
    JScrollPane jsTest;
    protected int pan;
    protected JFrame parent;
    protected int width             =120;
    protected int height            =186;   
    
    public PanListeCarte(String desc, int width, int height, int pan, JFrame parent) {
        // Taille
        this.width  = width;
        this.height = height;
        
        // JFrame parente
        this.parent = parent;
        
        this.pan = pan;
        
        // Paramètres du JPan
        this.setBackground(new java.awt.Color(110,48,34));
        setPreferredSize(new Dimension(this.width, this.height));
        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        
        // 2 JPannels inclus
        // 1. Pour le texte de desc
        this.pText = new JPanel();
        this.pText.setPreferredSize(new Dimension(width - 20, 25));
        this.pText.setOpaque(false);
        this.add(pText);
        
        // 2. Pour les cartes
        this.pCartes = new JPanel();
        this.pCartes.setSize(new Dimension(width - 20, height - 25 ));
        this.pCartes.setOpaque(false);
        
        //this.jsTest = new JScrollPane(this.pCartes);
        //this.pCartes.setPreferredSize(new Dimension(width *2, height*2));
        this.add(this.pCartes);
        
        // Description
        this.desc = new JLabel(desc);
        try {
            System.out.println(System.getProperties().get("user.dir"));
            Font font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/polices/cartoon.ttf"));
            font = font.deriveFont(18.0f);
            this.desc.setFont(font);
            this.desc.setForeground(new java.awt.Color(213, 151, 0));
        } catch (Exception ex) {
            Logger.getLogger(PanActionInit.class.getName()).log(Level.SEVERE, null, ex);
        }
//        Police police1 = new Police();
//        this.desc.setFont(new Police(3));
        
        
        this.pText.add(this.desc);
        
            
        this.cartes = new HashMap<PanCarte, Carte>();   
    }
    
   public PanListeCarte(ArrayList<Carte> cartes, String desc, int width, int height, int pan, JFrame parent, boolean selectable) {
        this( desc, width, height, pan, parent);
        
        PanCarte p;
        for ( Carte c : cartes ) {

            p = new PanCarte(c, parent, this, selectable);
            this.pCartes.add(p);
            this.cartes.put(p, c);
            
        }
    }

    public ArrayList<Carte> getCartes() {
        return (ArrayList<Carte>) cartes.values();
    }

    public void setCartes(ArrayList<Carte> cartes, boolean selectable) {
        this.pCartes.removeAll(); 
        this.cartes.clear();
        
        PanCarte p;
        for ( Carte c : cartes ) {
            p = new PanCarte(c, parent, this, selectable);                
            this.pCartes.add(p);
            this.cartes.put(p, c);
           
        }
        // Semble faire ce que j'esperais de repaint, donc pas toucher.
        // EDIT : Avec les deut, ça roule encore mieux
        this.pCartes.validate();  
        this.pCartes.repaint();
    }

    public JLabel getDesc() {
        return desc;
    }

    public void setDesc(JLabel desc) {
        this.desc = desc;
    }

    public Carte getSelected() {
        for (Map.Entry<PanCarte, Carte> e : ((HashMap<PanCarte, Carte>) this.cartes).entrySet()) {
            if( e.getKey().isSelected() )
                return e.getValue();
        }
        return null;
    }

    public void setSelected(PanCarte c) {
        if(c != null ) {
            for (Map.Entry<PanCarte, Carte> e : ((HashMap<PanCarte, Carte>) this.cartes).entrySet()) {
                if(! e.getKey().equals(c) )
                    e.getKey().setSelected(false);   
            }
        }
    }
   
}
